//
//  EeNumber.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/22.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import Foundation




public let EeNum = EeNumber.default
@objc public class EeNumber: NSObject {
    
    public struct Static {
        static var instance:EeNumber? = nil
        static var token:Int = 0
    }
    
    public static var `default`:EeNumber {
        if Static.instance == nil
        {
            Static.instance = EeNumber()
        }
        
        return Static.instance!
    }
    
    required override public init() {
        super.init()
        NotificationCenter.default.addObserver(self, selector:#selector(clearAll), name: JoUser.Notice.logout.name, object: nil)

    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func clearAll(){
        clear()
    }
    
    public let data_db =  DB.sqlite(db_name: "sysnum.db",bundle:Bundle(for: EeNumber.self))
    
    //    fileprivate let data_db = SQLiteDB(gid: "", db_name: "sysnum.db")//swift2
    var table_name = "number"
    
    
    
    public func read_state(_ id:String,type:EeMsgType) ->Bool
    {
        
        let sql = "select * from \(table_name) where id = '\(type.rawValue)_\(id)' "
        let data = data_db.query(sql: sql)
        //        debugPrint("query sql:\(sql)")
        
        var num = 0
        if data.count > 0 {
            
            for _ in data
            {
                num += 1
            }
        }
        
        
        if num > 0
        {
            return false
        }
        return true
    }
    
    public func delete(_ id:String,type:EeMsgType)
    {
        //        delete from number where id = 'order'
        let sql = "delete from number where id='\(type.rawValue)_\(id)'"
        //        debugPrint("delete sql:\(sql)")
        data_db.exec(sql: sql)
        NotificationCenter.default.post(name: type.Notice, object: nil)
        noticeToObsevers()
        
    }
    
    public func clear() {
        let sql = "delete from \(table_name);"
        data_db.exec(sql: sql)
        
        for one in EeMsgType.types
        {
            NotificationCenter.default.post(name: one.Notice, object: nil)
        }
        noticeToObsevers()
    }
    
    public func readAll(type:EeMsgType) {
        let sql = "delete from \(table_name) where id like '\(type.rawValue)%' "
        data_db.exec(sql: sql)
        NotificationCenter.default.post(name: type.Notice, object: nil)
        noticeToObsevers()
    }
    
    public  func new_item(_ id:String,type:EeMsgType)
    {
        let sql = "insert into \(table_name)(id) values('\(type.rawValue)_\(id)')"

        data_db.exec(sql: sql)
        debugPrint("insert sql:\(sql)")
        NotificationCenter.default.post(name: type.Notice, object: nil)
        noticeToObsevers()
    }
    
    
    public func num(_ type:EeMsgType) ->Int
    {
        //        let sql = "select count(*)from \(table_name) where id like '\(type.rawValue)%' "
        //        let data = data_db.query(sql)
        
        let sql1 = "select *from \(table_name) where id like '\(type.rawValue)%' "
        let data1 = data_db.query(sql: sql1)
        var num = 0
        if data1.count > 0 {
            
            for _ in data1
            {
                num += 1
            }
        }
        
        
        return num
        
    }
    
    public var observers = [EeNumObserver]()
    
    public func noticeToObsevers() {
        for one in self.observers
        {
            var nums = [(num:Int,msgType:EeMsgType)]()
            
            var all = 0
            for type in one.observerTypes
            {
                let num = self.num(type)
                let one_num = (num,type)
                all = all + num
                nums.append(one_num)
            }
            
            one.changed(numbers: nums, allCount: all)
        }
    }
  
}

public protocol EeNumObserver {
    var observerTypes:[EeMsgType]{get set}
    func changed(numbers:[(num:Int,msgType:EeMsgType)],allCount:Int)
}


